package cn.trip.controller;


import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.trip.pojo.Trip_product;
import cn.trip.pojo.Trip_provider;
import cn.trip.pojo.Trip_root;
import cn.trip.service.provider.ProviderService;
import cn.trip.service.root.RootService;
import cn.trip.utils.Constants;
import cn.trip.utils.PageSupport;


@Controller
@RequestMapping("/root")
public class RootController {
	
	private Logger logger = Logger.getLogger(RootController.class);
	
	@Resource
	private ProviderService providerService;
	
	@Resource
	private RootService rootService;
	
	@RequestMapping(value="/login")
	public String login(){
		return "root/login";
	}
	
	@RequestMapping(value="/root.html")
	public String root(Model model,@ModelAttribute Trip_root root,HttpSession session){
		Trip_root tr = rootService.getRootList(root.getRoleCode(), root.getRolePassword());
		if(tr==null){
			model.addAttribute("error","账号或者密码错误!");
			return "login";
		}
		session.setAttribute(Constants.ROOT_SESSION, tr);
		return "redirect:/root/flatform/main";
	}
	
	@RequestMapping(value="/flatform/main")
	public String main(HttpSession session){
		if(session.getAttribute(Constants.ROOT_SESSION)==null){
			return "redirect:/root/login";
		}
		return "root/main";
	}
	
	@RequestMapping(value="/exist")
	public String exist(HttpSession session){
		session.removeAttribute(Constants.ROOT_SESSION);
		return "redirect:/index/index.do";
	}
	
	@RequestMapping(value="/modify")
	public String modify(){
		return "root/update";
	}
	
	@RequestMapping(value="/panduan")
	@ResponseBody
	public Object panduan(@RequestParam("password")String password,HttpSession session){
		String data = "";
		Trip_root root = (Trip_root) session.getAttribute(Constants.ROOT_SESSION);
		System.out.println(password);
		System.out.println(root.getRolePassword());
		if(root.getRolePassword().equals(password) || root.getRolePassword()==password){
			data = "true";
		}else{
			data = "false";
		}
		return data;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(@RequestParam("roleName")String roleName,Model model,@RequestParam("newPassword")String newPassword,HttpSession session){
		Trip_root root = (Trip_root) session.getAttribute(Constants.ROOT_SESSION);
		logger.debug("code===================>"+root.getRoleCode());
		logger.debug("password===================>"+root.getRolePassword());
		logger.debug("roleName======================>"+root.getRoleName());
		if(rootService.modify(root.getRoleCode(),newPassword,roleName)){
			return "redirect:/root/login";
		}
		model.addAttribute("error", "修改失败,请确认信息填写是否正确!!!");
		return "root/update";
	}
	
	@RequestMapping(value="selectinfo")
	public String show(Model model,@RequestParam(value="proName",required=false)String proName,
				@RequestParam(value="pageIndex",required=false) String pageIndex){
		logger.debug("proName==================>"+proName);
		logger.debug("pageIndex==================>"+pageIndex);
		List<Trip_provider> list = null;
		List<Trip_product> hotelList = null;
		List<Trip_product> fieldList = null;
		//页面容量
		int pageSize = Constants.pageSize;
		//当前页码
		Integer currentPageNo =1;
		
		if(pageIndex != null){
			try{
				currentPageNo = Integer.valueOf(pageIndex);
			}catch (NumberFormatException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		//总数量（表）
		int totalCount = 0;
		try {
			totalCount = providerService.providerCount(proName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//总页数
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		//控制首页和尾页
		if(currentPageNo < 1){
			currentPageNo = 1;
		}else if(currentPageNo > totalPageCount){
			currentPageNo = totalPageCount;
		}
		try {
			list = providerService.getProviderList(proName, currentPageNo, pageSize);
			hotelList = rootService.getHotelList();
			fieldList = rootService.getFieldList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("list",list);
		model.addAttribute("hotelList",hotelList);
		model.addAttribute("fieldList",fieldList);
		model.addAttribute("pages", pages);
		return "root/root";
	}
	
	@RequestMapping(value="/show")
	public String show1(Model model,@RequestParam(value="proName",required=false)String createBy,
			@RequestParam(value="pageIndex",required=false) String pageIndex){
		logger.debug("createBy==================>"+createBy);
		logger.debug("pageIndex==================>"+pageIndex);
		List<Trip_product> list = null;
		//页面容量
		int pageSize = Constants.pageSize;
		//当前页码
		Integer currentPageNo =1;
		
		if(pageIndex != null){
			try{
				currentPageNo = Integer.valueOf(pageIndex);
			}catch (NumberFormatException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		//总数量（表）
		int totalCount = 0;
		try {
			totalCount = rootService.getListCount(createBy);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//总页数
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		//控制首页和尾页
		if(currentPageNo < 1){
			currentPageNo = 1;
		}else if(currentPageNo > totalPageCount){
			currentPageNo = totalPageCount;
		}
		try {
			list = rootService.getList(createBy, currentPageNo, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("list",list);
		model.addAttribute("createBy",createBy);
		model.addAttribute("pages", pages);
		return "root/show";
	}
	
	@RequestMapping(value="/showproduct")
	public String showProduct(@RequestParam(value="id",required=false)String _id,
				Model model,@RequestParam(value="createBy",required=false)String createBy){
		Integer id = 0;
		if(_id!=null){
			id = Integer.parseInt(_id);
		}
		Trip_product product = rootService.getProduct(id);
		model.addAttribute("createBy",createBy);
		model.addAttribute("list",product);
		model.addAttribute("id",_id);
		return "root/showInfo";
	}
	
	@RequestMapping(value="/updatestatus")
	public String updatestatus(@RequestParam(value="status",required=false)String _status,
							@RequestParam(value="id",required=false)String _id,Model model,
							@RequestParam(value="createBy")String createBy){
		logger.debug("status================>"+_status);
		logger.debug("id====================>"+_id);
		logger.debug("createBy================>"+createBy);
		Integer status = 0;
		Integer id = 0;
		
		if(_status != null){
			status = Integer.parseInt(_status);
			if(status==1){
				status = 2;
			}else{
				status =1;
			}
		}
		if(_id != null){
			id = Integer.parseInt(_id);
		}
		if(rootService.updateStatus(status, id)){
			model.addAttribute("proName",createBy);
			return "redirect:/root/show";
		}
		model.addAttribute("error","操作失败! 请咨询有关部门!");
		return "root/show";
	}
}
